//
//  KATTableCellData.m
//  KATFramework
//
//  Created by Kantice on 15/8/5.
//  Copyright (c) 2015年 KatApp. All rights reserved.
//

#import "KATTableCellData.h"
#import "KATImage.h"


@implementation KATTableCellData

+ (instancetype)cellData
{
    return [[[self alloc] init] autorelease];
}


//初始化
- (instancetype)init
{
    self=[super init];
    
    self.type=0;
    self.num=0;
    self.tag=0;
    self.index=0;
    self.value=0.0;
    self.state=TABLE_CELL_STATE_READY;
    self.message=nil;
    self.message2=nil;
    self.object=nil;
    self.image=nil;
    self.imagePath=nil;
    self.hasImageFrame=NO;
    self.image2=nil;
    self.image2Path=nil;
    self.hasImage2Frame=NO;
    self.image3=nil;
    self.image3Path=nil;
    self.hasImage3Frame=NO;
    self.title=nil;
    self.hasTitleFrame=NO;
    self.content=nil;
    self.hasContentFrame=NO;
    self.richTitle=nil;
    self.hasRichTitleFrame=NO;
    self.richTitleStyle=nil;
    self.hasRichTitleStyle=NO;
    self.richContent=nil;
    self.hasRichContentFrame=NO;
    self.richContentStyle=nil;
    self.hasRichContentStyle=NO;
    self.richLabel=nil;
    self.label1=nil;
    self.label2=nil;
    self.label3=nil;
    self.label4=nil;
    self.label5=nil;
    self.text=nil;
    self.hasTextFrame=NO;
    self.badge=nil;
    self.hasBadgeFrame=NO;
    self.backLabel=nil;
    self.hasBackLabelFrame=NO;
    self.hasBadge=NO;
    self.hasImage=NO;
    self.hasImageMemory=NO;
    self.hasImage2=NO;
    self.hasImage2Memory=NO;
    self.hasImage3=NO;
    self.hasImage3Memory=NO;
    self.badgeBgColor=nil;
    self.hasBadgeBgColor=NO;
    self.badgeTextColor=nil;
    self.hasBadgeTextColor=NO;
    self.hasBadgeBorderColor=NO;
    self.badgeBorderColor=nil;
    self.backBgColor=nil;
    self.hasBackBgColor=NO;
    self.midBgColor=nil;
    self.hasMidBgColor=NO;
    self.faceBgColor=nil;
    self.hasFaceBgColor=NO;
    self.titleColor=nil;
    self.hasTitleColor=NO;
    self.titleFont=nil;
    self.hasTitleFont=NO;
    self.contentColor=nil;
    self.hasContentColor=NO;
    self.contentFont=nil;
    self.hasContentFont=NO;
    self.label1Color=nil;
    self.hasLabel1Color=NO;
    self.label2Color=nil;
    self.hasLabel2Color=NO;
    self.label3Color=nil;
    self.hasLabel3Color=NO;
    self.label4Color=nil;
    self.hasLabel4Color=NO;
    self.label5Color=nil;
    self.hasLabel5Color=NO;
    self.backLabelColor=nil;
    self.hasBackLabelColor=NO;
    self.backLabelFont=nil;
    self.hasBackLabelFont=NO;
    self.textColor=nil;
    self.hasTextColor=NO;
    self.textBgColor=nil;
    self.hasTextBgColor=NO;
    self.textFont=nil;
    self.hasTextFont=NO;
    self.hasSpButton=YES;
    self.spButtonColor=nil;
    self.spButtonLineWidth=1.0f;
    self.spButtonType=KATButtonTypeNone;
    self.spButtonScale=1.0;
    self.isRemoteImage=NO;
    self.isCacheImage=NO;
    self.isImageReady=NO;
    self.isLoadImageFail=NO;
    self.isRemoteImage2=NO;
    self.isCacheImage2=NO;
    self.isImage2Ready=NO;
    self.isLoadImage2Fail=NO;
    self.isRemoteImage3=NO;
    self.isCacheImage3=NO;
    self.isImage3Ready=NO;
    self.isLoadImage3Fail=NO;
    
    return self;
}


//远程加载图片(异步)(path为空时使用KATImage的缓存路径)
- (void)loadRemoteImageWithSavePath:(NSString *)path andUrl:(NSString *)url
{
    //设置标识量
    _hasImage=YES;
    _isRemoteImage=YES;
    _isCacheImage=YES;
    _isLoadImageFail=NO;
    _isImageReady=NO;
    
    //默认的path
    if(!path)
    {
        path=[NSString stringWithFormat:@"%@/%@",[KATImage cacheDir],[KATImage fileNameWithUrl:url]];
    }
    
    //先判断是否本地已保存该图片
    if([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:false])//存在该文件，则不需要下载
    {
        self.imagePath=path;
        
        if(_hasImageMemory)//需要创建图片
        {
            self.image=[UIImage imageWithContentsOfFile:_imagePath];
        }
        
        _isImageReady=YES;
    }
    else//不存在该文件，则去下载
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^
        {
            [self retain];
            
           _isLoadImageFail=!([KATHttpUtil downloadFile:path fromUrl:url]);
           
           if(!_isLoadImageFail)//下载成功
           {
               self.imagePath=path;
               
               if(_hasImageMemory)//需要创建图片
               {
                   self.image=[UIImage imageWithContentsOfFile:_imagePath];
               }
           }
           else if([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:false])//如果已经存在该图片（其他线程下载）
           {
               _isLoadImageFail=NO;
           }
            
            [self release];
           
           _isImageReady=YES;            
        });
    }
}




//远程加载图片2(异步)(path为空时使用KATImage的缓存路径)
- (void)loadRemoteImage2WithSavePath:(NSString *)path andUrl:(NSString *)url
{
    //设置标识量
    _hasImage2=YES;
    _isRemoteImage2=YES;
    _isCacheImage2=YES;
    _isLoadImage2Fail=NO;
    _isImage2Ready=NO;
    
    //默认的path
    if(!path)
    {
        path=[NSString stringWithFormat:@"%@/%@",[KATImage cacheDir],[KATImage fileNameWithUrl:url]];
    }
    
    //先判断是否本地已保存该图片
    if([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:false])//存在该文件，则不需要下载
    {
        self.image2Path=path;
        
        if(_hasImage2Memory)//需要创建图片
        {
            self.image2=[UIImage imageWithContentsOfFile:_image2Path];
        }
        
        _isImage2Ready=YES;
    }
    else//不存在该文件，则去下载
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^
        {
            [self retain];
            
           _isLoadImage2Fail=!([KATHttpUtil downloadFile:path fromUrl:url]);
           
           if(!_isLoadImage2Fail)//下载成功
           {
               self.image2Path=path;
               
               if(_hasImage2Memory)//需要创建图片
               {
                   self.image2=[UIImage imageWithContentsOfFile:_image2Path];
               }
           }
           else if([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:false])//如果已经存在该图片（其他线程下载）
           {
               _isLoadImage2Fail=NO;
           }
           
            [self release];
            
           _isImage2Ready=YES;
        });
    }
}



//远程加载图片3(异步)(path为空时使用KATImage的缓存路径)
- (void)loadRemoteImage3WithSavePath:(NSString *)path andUrl:(NSString *)url
{
    //设置标识量
    _hasImage3=YES;
    _isRemoteImage3=YES;
    _isCacheImage3=YES;
    _isLoadImage3Fail=NO;
    _isImage3Ready=NO;
    
    //默认的path
    if(!path)
    {
        path=[NSString stringWithFormat:@"%@/%@",[KATImage cacheDir],[KATImage fileNameWithUrl:url]];
    }
    
    //先判断是否本地已保存该图片
    if([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:false])//存在该文件，则不需要下载
    {
        self.image3Path=path;
        
        if(_hasImage3Memory)//需要创建图片
        {
            self.image3=[UIImage imageWithContentsOfFile:_image3Path];
        }
        
        _isImage3Ready=YES;
    }
    else//不存在该文件，则去下载
    {
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^
        {
            [self retain];
            
           _isLoadImage3Fail=!([KATHttpUtil downloadFile:path fromUrl:url]);
           
           if(!_isLoadImage3Fail)//下载成功
           {
               self.image3Path=path;
               
               if(_hasImage3Memory)//需要创建图片
               {
                   self.image3=[UIImage imageWithContentsOfFile:_image3Path];
               }
           }
           else if([[NSFileManager defaultManager] fileExistsAtPath:path isDirectory:false])//如果已经存在该图片（其他线程下载）
           {
               _isLoadImage3Fail=NO;
           }
            
            [self release];
           
           _isImage3Ready=YES;
        });
    }
}



//描述
- (NSString *)description
{
    return [NSString stringWithFormat:@"Cell Data:%i Title:%@!",_num,_title];
}


- (void)dealloc
{
//    NSLog(@"Cell Data:%i (%@) is delloc!",_num,_title);
    
    [_message release];
    [_message2 release];
    [_object release];
    [_image release];
    [_imagePath release];
    [_image2 release];
    [_image2Path release];
    [_image3 release];
    [_image3Path release];
    [_title release];
    [_content release];
    [_richTitle release];
    [_richTitleStyle release];
    [_richContent release];
    [_richContentStyle release];
    [_richLabel release];
    [_label1 release];
    [_label2 release];
    [_label3 release];
    [_label4 release];
    [_label5 release];
    [_text release];
    [_textBgColor release];
    [_textColor release];
    [_textFont release];
    [_badge release];
    [_backLabel release];
    [_badgeBgColor release];
    [_badgeTextColor release];
    [_badgeBorderColor release];
    [_backBgColor release];
    [_midBgColor release];
    [_faceBgColor release];
    [_titleColor release];
    [_titleFont release];
    [_contentColor release];
    [_contentFont release];
    [_label1Color release];
    [_label2Color release];
    [_label3Color release];
    [_label4Color release];
    [_label5Color release];
    [_backLabelColor release];
    [_backLabelFont release];
    [_spButtonColor release];
    
    [super dealloc];
}

@end
